
from rest_framework import serializers
from .models import LocationRecord, Milestone, TrackingSettings
from order_app.serializers import OrderListSerializer
from vehicle_app.serializers import VehicleListSerializer


class LocationRecordSerializer(serializers.ModelSerializer):
    """位置记录序列化器"""
    
    order = OrderListSerializer(read_only=True)
    vehicle = VehicleListSerializer(read_only=True)
    
    class Meta:
        model = LocationRecord
        fields = [
            'id', 'order', 'vehicle', 'longitude', 'latitude', 'altitude',
            'accuracy', 'speed', 'direction', 'address', 'province', 'city',
            'district', 'created_at'
        ]
        read_only_fields = ['id', 'created_at']


class LocationRecordCreateSerializer(serializers.ModelSerializer):
    """位置记录创建序列化器"""
    
    class Meta:
        model = LocationRecord
        fields = [
            'order', 'longitude', 'latitude', 'altitude',
            'accuracy', 'speed', 'direction', 'address',
            'province', 'city', 'district'
        ]


class MilestoneSerializer(serializers.ModelSerializer):
    """里程碑序列化器"""
    
    order = OrderListSerializer(read_only=True)
    milestone_type_display = serializers.CharField(source='get_milestone_type_display', read_only=True)
    
    class Meta:
        model = Milestone
        fields = [
            'id', 'order', 'milestone_type', 'milestone_type_display',
            'description', 'longitude', 'latitude', 'address', 'created_at'
        ]
        read_only_fields = ['id', 'milestone_type_display', 'created_at']


class MilestoneCreateSerializer(serializers.ModelSerializer):
    """里程碑创建序列化器"""
    
    class Meta:
        model = Milestone
        fields = [
            'order', 'milestone_type', 'description',
            'longitude', 'latitude', 'address'
        ]


class TrackingSettingsSerializer(serializers.ModelSerializer):
    """跟踪设置序列化器"""
    
    vehicle = VehicleListSerializer(read_only=True)
    
    class Meta:
        model = TrackingSettings
        fields = [
            'id', 'vehicle', 'report_interval', 'report_distance',
            'power_saving_mode', 'power_saving_interval',
            'is_tracking_enabled', 'created_at', 'updated_at'
        ]
        read_only_fields = ['id', 'created_at', 'updated_at']


class TrackingSettingsUpdateSerializer(serializers.ModelSerializer):
    """跟踪设置更新序列化器"""
    
    class Meta:
        model = TrackingSettings
        fields = [
            'report_interval', 'report_distance',
            'power_saving_mode', 'power_saving_interval',
            'is_tracking_enabled'
        ]